//
//  ViewController.m
//  09-UITableViewEdit
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#define kTableViewReuse @"reuseIndentifier"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *nameArr;
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> *selectIndexArray;
@property (nonatomic, strong) NSMutableArray *selcetNameArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *arr = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.nameArr = [NSMutableArray arrayWithArray:arr];
    self.selectIndexArray = [NSMutableArray array];
    self.selcetNameArray = [NSMutableArray array];
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    // tableView编辑
    // 1 设置右batButtonItem 为编辑的editButtonItem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    // 编辑按钮的点击方法
    NSLog(@"%@", NSStringFromSelector(self.editButtonItem.action));
    self.navigationItem.rightBarButtonItem.action = @selector(editAction);
}

- (void)editAction {
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
}

#pragma mark - tableView Delegate
// 4 返回cell的编辑状态(样式) 默认为Delete
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 返回UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete 多选
    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        // 在全选状态下, 数组中添加对应选中的下标
        [_selectIndexArray addObject:indexPath];
        // 把选中的cell的内容存到人名数组
        [_selcetNameArray addObject:_nameArr[indexPath.row]];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        // 在全选状态下 数组中移除对应选中的下标
        [_selectIndexArray removeObject:indexPath];
        
        NSString *name = _nameArr[indexPath.row];
        [_selcetNameArray addObject:name];
    }
}
#pragma mark - tableView DataSource
// 2 让tableView进入编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"bianji");
    } else {
        [_nameArr removeObjectsInArray:_selcetNameArray];
        [_tableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationAutomatic];
        [_selectIndexArray removeAllObjects];
        [_selcetNameArray removeAllObjects];
        NSLog(@"done");
    }
}
// 3 控制tableView能否进行编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 能否移动 默认为可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 5 对应编辑状态(editingStyle)的点击方法
// 滑动删除, 只和这个方法有关系
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // !! 如果对tableView进行添加/删除, 先修改数据源 再去进行对应的添加/删除
        [_nameArr removeObjectAtIndex:indexPath.row];
        // 对应修改cell
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        [_nameArr insertObject:@"213" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    // tableView无论删除还是添加, 都已经进行了刷新操作
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    [_nameArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}
// DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nameArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kTableViewReuse];
    }
    cell.textLabel.text = _nameArr[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
